<?php
/**
 * Created by PhpStorm.
 * User: l
 * Date: 2018/10/24
 * Time: 17:50
 */
namespace app\models;
use yii\db\ActiveRecord;

class Dynamic_comment extends ActiveRecord {
    public function getFirstComments($dynamic_id,$limit,$offset){
        $post = Dynamic_comment::find()->where(['dynamic_comment.dynamic_id' => $dynamic_id,'dynamic_comment.comment_level'=>1,'dynamic_comment.is_deleted'=>0]);

        $count = $post->count();
        $result = $post->JoinWith('user')->orderBy('dynamic_comment.comment_id DESC')->limit($limit)->offset($offset)->asArray()->all();

        return ['total'=>$count, 'rows'=>$result];
    }

    public function getSecondComments($comment_id,$limit,$offset){
        $post = Dynamic_comment::find()->where(['dynamic_comment.first_comment_id' => $comment_id,'dynamic_comment.is_deleted'=>0]);

        $count = $post->count();
        $result = $post->JoinWith(['user'])->orderBy('dynamic_comment.comment_id DESC')->limit($limit)->offset($offset)->asArray()->all();

        return ['total'=>$count, 'rows'=>$result];
    }

    public function getCommentData($comment_id){
        $comment = Dynamic_comment::find()->where(['dynamic_comment.comment_id' => $comment_id])->JoinWith(['user'])->asArray()->one();
        return $comment;
    }

    public function changeCommentStatus($commentId,$type){
        $post = Dynamic_comment::findOne($commentId);
        $post->is_deleted = $type;
        return $post->save();
    }

    //关联用户表 获取用户信息
    public function getUser()
    {
        //同样第一个参数指定关联的子表模型类名
        //第二个参数第一个是字表的 第二个是主表的
        return $this->hasOne(User_user::className(), ['user_id' => 'user_id']);
    }
}